<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\App;

class UserResource extends JsonResource
{
  protected $formats;

  public function toArray($request)
  {
    $this->formats = config('coreapi.' . App::getLocale());

    return [
      'id' => $this->id,
      'name' => $this->name,
      'email' => $this->email,
      //'password' => $this->password,
      'email_verified_at' => isset($this->email_verified_at) ? localizedFormat($this->email_verified_at, 'datetime') : null,
      'active' => $this->active,
      'registered' => $this->registered,
      'last_login_at' => isset($this->last_login_at) ? localizedFormat($this->last_login_at, 'datetime') : null,
      'last_logout_at' => isset($this->last_logout_at) ? localizedFormat($this->last_logout_at, 'datetime') : null,
      'created_at' => localizedFormat($this->created_at, 'datetime'),
      'updated_at' => localizedFormat($this->updated_at, 'datetime'),
      'role' => new RoleResource($this->whenLoaded('role')),
      'person' => new PersonResource($this->whenLoaded('person')),
      'company' => new CompanyResource($this->whenLoaded('company')),
      'addresses' => AddressResource::collection($this->whenLoaded('addresses')),
      'telephones' => AddressResource::collection($this->whenLoaded('telephones')),
      'emails' => EmailResource::collection($this->whenLoaded('emails')),
      'picture' => new PictureResource($this->whenLoaded('picture')),
    ];
  }
}
